/**
 * 内容管理相关逻辑
 * @type {*}
 */
var recordNo = $('#recordNo').val();
var dataContent = '';

/**
 * 初始化分页
 */
function init() {
    $("#disappare").show();

    $.ajax({
        url: "/manage/site/listSel",
        type: "post",
        data: {

        },
        success: function(data){
            console.log("调用成功");
            if (data.status) {

                for (var i = 0; i < data.data.length; i++) {
                    $("#site").append("<option value='" + data.data[i].id + "'>" + data.data[i].name + "</option>");
                }
            } else {
               alert("加载站点列表出错,操作失败!");
            }
        },
        fail: function(data) {
            console.error("服务器内部错误");
        }
    });

    $.ajax({
        url: "/manage/readedit/details",
        type: 'POST',
        data: {
            recordNo: recordNo
        },
        success: function (cData) {
            if (cData.status == false){
                $("#disappare").hide();
                alert(cData.message);
                window.close();
            } else {
                $("#disappare").hide();
                loadDetail(cData, recordNo);
            }
        }
    });
}

/**
 * 加载编辑信息
 * @param cData
 */
function loadDetail(cData) {
    var data = cData.data;

    var id = data.id;
    var title = data.title;
    var originalUrl = data.originalUrl;
    var releaseTime = data.releaseTime;
    var site = data.site;
    var author = data.author;
    var content = data.content;

    $("#id").val(getEffectiveContent(id));
    $("#title").val(getEffectiveContent(title));
    $("#originalUrl").val(getEffectiveContent(originalUrl));
    $("#releaseTime").val(getEffectiveContent(releaseTime));
    $("#site").val(getEffectiveContent(site));
    $("#author").val(getEffectiveContent(author));

    UE.delEditor("content");
    var ue = UE.getEditor("content");
    ue.ready(function() {
        ue.execCommand("inserthtml", content);
    });

    ue.addListener("contentChange",function(){
        dataContent = ue.getContent();
    });
}

//检查输入的内容
function submitDataForEdit() {
    var _id = $("#id"),
        _title = $("#title");
        _originalUrl = $("#originalUrl");
        _releaseTime = $("#releaseTime");
        _site = $("#site");
        _author = $("#author");

    $.ajax({
        url: "/manage/readedit/save",
        type: "post",
        data: {
            id: _id.val(),
            recordNo: recordNo,
            title: _title.val(),
            originalUrl: _originalUrl.val(),
            releaseTime: _releaseTime.val(),
            site: _site.val(),
            author: _author.val(),
            content: dataContent
        },
        success: function(data){
            if (data.status) {
                alert("内容编辑成功");
                window.close();
                return true;
            } else {
                alert("内容编辑失败");
                return false;
            }
        },
        fail: function(data) {
            console.error("服务器内部错误");
        }
    });
}

/**
 * 获取有效信息
 * @param content
 * @returns {*}
 */
function getEffectiveContent(content) {
    if (content == null || content == undefined) {
        return '';
    }else {
        return content;
    }
}



